package cap.action;

import cap.bean.Article;
import cap.bean.SysCategory;
import cap.bean.User;
import cap.dao.ArticleDao;
import cap.dao.SysCategoryDao;
import cap.dao.impl.ArticleDaoImpl;
import cap.dao.impl.SysCategoryDaoImpl;
import cap.util.PageControl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/index")
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ArticleDao artDao;
	private SysCategoryDao scDao;
	public IndexServlet() {
		artDao=new ArticleDaoImpl();
		scDao=new SysCategoryDaoImpl();
	}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		String method=request.getParameter("method");
		if(method!=null&&method.equals("indexFilter")){//过滤博文
			List<SysCategory> scList=scDao.getAllSysCategory();//获取系统分类列表
			List<User> uList=artDao.getActiveUser(2); //获取 2 个活跃人数
			List<Article> tenList=artDao.topTenArticle();//获取博文top10
			//分页
			String curPageStr = request.getParameter("curPage");
			int sysCategoryId = Integer.parseInt(request.getParameter("sysCategoryId"));
			int count=artDao.getAllArtical().size();
			int curPage=1;
			if(curPageStr!=null){
				curPage=Integer.parseInt(curPageStr);
			}
			PageControl pc = new PageControl(curPage, count);//获取当前页数博文数据
			List<Article> artList= artDao.getFilterArticleByPage(pc.getCurPage(), count,sysCategoryId);
			pc.setList(artList);
			request.setAttribute("curPage", pc.getCurPage());
			request.setAttribute("totalPages", pc.getTotalPages());
			request.setAttribute("uList", uList);
			request.setAttribute("scList", scList);
			request.setAttribute("tenList", tenList);
			request.setAttribute("artList", pc.getList());
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}else{
			List<SysCategory> scList=scDao.getAllSysCategory();//获取系统分类列表
			List<User> uList=artDao.getActiveUser(2); //获取 2 个活跃人数
			List<Article> tenList=artDao.topTenArticle();//获取博文top10
			//分页
			String curPageStr = request.getParameter("curPage");
			int count=artDao.getAllArtical().size();
			int curPage=1;
			if(curPageStr!=null){
				curPage=Integer.parseInt(curPageStr);
			}
			PageControl pc = new PageControl(curPage, count);//获取当前页数博文数据
			List<Article> artList= artDao.getArticleByPage(curPage, count);
			pc.setList(artList);
			//Counter counter=counterService.getCounter();//网站计数
			request.setAttribute("curPage", pc.getCurPage());
			request.setAttribute("totalPages", pc.getTotalPages());
			request.setAttribute("uList", uList);
			request.setAttribute("scList", scList);
			request.setAttribute("tenList", tenList);
			//request.setAttribute("counter", counter);
			request.setAttribute("artList", pc.getList());
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}
	}
}
